package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonth(List<String> months) {
        //定义容器保存每个月的会员数量情况
        List<Integer> memberCount = new ArrayList<Integer>();
        //遍历获取每个月的会员数量情况
        months.forEach(month->{
            Integer count = memberDao.findMemberCountBeforeDate(month);
            memberCount.add(count);
        });
        return memberCount;
    }


    /**
     * 会员年龄分布数量
     * @return
     */
    @Override
    public List<Integer> findMemberCountByAge() {
//        List<Integer> memberCount = new ArrayList<>();
        return memberDao.findMemberCountByAge();
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Member> page = memberDao.selectByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        memberDao.deleteById(id);
    }

    @Override
    public Member findById(int id) {
        return null;
    }

    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void edit(Member member) { memberDao.update(member); }

    @Override
    public List<Member> findAll() {
        return memberDao.findAll();
    }

    @Override
    public void update(Member member) { memberDao.update(member); }

}
